layui.use(["layer","laydate","form","jquery","table"],function () {
    var
        layer = layui.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        table = layui.table,
        form = layui.form;
    TotalCourseStatusCalculations(3,2);
    TotalCourseStatusCalculations(3,3);
    TotalCourseStatusCalculations(3,4);
    TotalCourseStatusCalculations(3,5);
    TotalCourseStatusCalculations(3,6);
    TotalCourseStatusCalculations(3,7);
    TotalCourseStatusCalculations(3,9);
    TotalCourseStatusCalculations(3,10);
    TotalCourseStatusCalculations(3,11);
    //所有商品
    $("#selectAllProduct").click(function () {
        $("#selectAllProduct").css("backgroundColor","#009688");
        $("#selectAllProduct").css("color","#fff");
        $("#AllProduct").css("color","#fff");

        $("#selectUnpaidProduct").css("backgroundColor","white");
        $("#selectUnpaidProduct").css("color","green");
        $("#unpaid").css("color","green");


        $("#selectPaidProduct").css("backgroundColor","white");
        $("#selectPaidProduct").css("color","green");
        $("#Paid").css("color","green");


        $("#selectToBeShippedProduct").css("backgroundColor","white");
        $("#selectToBeShippedProduct").css("color","green");
        $("#ToBeShipped").css("color","green");


        $("#selectShippedProduct").css("backgroundColor","white");
        $("#selectShippedProduct").css("color","green");
        $("#Shipped").css("color","green");

        $("#selectSignedInProduct").css("backgroundColor","white");
        $("#selectSignedInProduct").css("color","green");
        $("#SignedIn").css("color","green");

        $("#selectCancelTransactionProduct").css("backgroundColor","white");
        $("#selectCancelTransactionProduct").css("color","green");
        $("#CancelTransaction").css("color","green");

        getAllCourseOrder(param);
    });
    //未付款商品
    $("#selectUnpaidProduct").click(function () {
        var param = {
            "orderType":"3",
            "orderStatus":2
        }
        $("#selectUnpaidProduct").css("backgroundColor","#009688");
        $("#selectUnpaidProduct").css("color","#fff");
        $("#unpaid").css("color","#fff");

        $("#selectAllProduct").css("backgroundColor","white");
        $("#selectAllProduct").css("color","green");
        $("#AllProduct").css("color","green");


        $("#selectPaidProduct").css("backgroundColor","white");
        $("#selectPaidProduct").css("color","green");
        $("#Paid").css("color","green");


        $("#selectToBeShippedProduct").css("backgroundColor","white");
        $("#selectToBeShippedProduct").css("color","green");
        $("#ToBeShipped").css("color","green");


        $("#selectShippedProduct").css("backgroundColor","white");
        $("#selectShippedProduct").css("color","green");
        $("#Shipped").css("color","green");

        $("#selectSignedInProduct").css("backgroundColor","white");
        $("#selectSignedInProduct").css("color","green");
        $("#SignedIn").css("color","green");

        $("#selectCancelTransactionProduct").css("backgroundColor","white");
        $("#selectCancelTransactionProduct").css("color","green");
        $("#CancelTransaction").css("color","green");
        //未付款
        QueryProductOrdersByStatus(param);
    });
    //已付款商品
    $("#selectPaidProduct").click(function () {
        var param = {
            "orderType":"3",
            "orderStatus":3
        }
        $("#selectPaidProduct").css("backgroundColor","#009688");
        $("#selectPaidProduct").css("color","#fff");
        $("#Paid").css("color","#fff");

        $("#selectAllProduct").css("backgroundColor","white");
        $("#selectAllProduct").css("color","green");
        $("#AllProduct").css("color","green");


        $("#selectUnpaidProduct").css("backgroundColor","white");
        $("#selectUnpaidProduct").css("color","green");
        $("#unpaid").css("color","green");


        $("#selectToBeShippedProduct").css("backgroundColor","white");
        $("#selectToBeShippedProduct").css("color","green");
        $("#ToBeShipped").css("color","green");


        $("#selectShippedProduct").css("backgroundColor","white");
        $("#selectShippedProduct").css("color","green");
        $("#Shipped").css("color","green");

        $("#selectSignedInProduct").css("backgroundColor","white");
        $("#selectSignedInProduct").css("color","green");
        $("#SignedIn").css("color","green");

        $("#selectCancelTransactionProduct").css("backgroundColor","white");
        $("#selectCancelTransactionProduct").css("color","green");
        $("#CancelTransaction").css("color","green");
        //未付款
        QueryProductOrdersByStatus(param);
    });
    //待发货
    $("#selectToBeShippedProduct").click(function () {
        var param = {
            "orderType":"3",
            "orderStatus":4
        }
        $("#selectToBeShippedProduct").css("backgroundColor","#009688");
        $("#selectToBeShippedProduct").css("color","#fff");
        $("#ToBeShipped").css("color","#fff");

        $("#selectAllProduct").css("backgroundColor","white");
        $("#selectAllProduct").css("color","green");
        $("#AllProduct").css("color","green");


        $("#selectUnpaidProduct").css("backgroundColor","white");
        $("#selectUnpaidProduct").css("color","green");
        $("#unpaid").css("color","green");


        $("#selectPaidProduct").css("backgroundColor","white");
        $("#selectPaidProduct").css("color","green");
        $("#Paid").css("color","green");


        $("#selectShippedProduct").css("backgroundColor","white");
        $("#selectShippedProduct").css("color","green");
        $("#Shipped").css("color","green");

        $("#selectSignedInProduct").css("backgroundColor","white");
        $("#selectSignedInProduct").css("color","green");
        $("#SignedIn").css("color","green");

        $("#selectCancelTransactionProduct").css("backgroundColor","white");
        $("#selectCancelTransactionProduct").css("color","green");
        $("#CancelTransaction").css("color","green");
        //未付款
        QueryProductOrdersByStatus(param);
    });
    //已发货商品
    $("#selectShippedProduct").click(function () {
        var param = {
            "orderType":"3",
            "orderStatus":5
        }
        //未付款
        $("#selectShippedProduct").css("backgroundColor","#009688");
        $("#selectShippedProduct").css("color","#fff");
        $("#Shipped").css("color","#fff");

        $("#selectAllProduct").css("backgroundColor","white");
        $("#selectAllProduct").css("color","green");
        $("#AllProduct").css("color","green");


        $("#selectUnpaidProduct").css("backgroundColor","white");
        $("#selectUnpaidProduct").css("color","green");
        $("#unpaid").css("color","green");


        $("#selectPaidProduct").css("backgroundColor","white");
        $("#selectPaidProduct").css("color","green");
        $("#Paid").css("color","green");


        $("#selectToBeShippedProduct").css("backgroundColor","white");
        $("#selectToBeShippedProduct").css("color","green");
        $("#ToBeShipped").css("color","green");

        $("#selectSignedInProduct").css("backgroundColor","white");
        $("#selectSignedInProduct").css("color","green");
        $("#SignedIn").css("color","green");

        $("#selectCancelTransactionProduct").css("backgroundColor","white");
        $("#selectCancelTransactionProduct").css("color","green");
        $("#CancelTransaction").css("color","green");
        QueryProductOrdersByStatus(param);
    });
    //已签收商品
    $("#selectSignedInProduct").click(function () {
        var param = {
            "orderType":"3",
            "orderStatus":6
        }
        $("#selectSignedInProduct").css("backgroundColor","#009688");
        $("#selectSignedInProduct").css("color","#fff");
        $("#SignedIn").css("color","#fff");

        $("#selectShippedProduct").css("backgroundColor","white");
        $("#selectShippedProduct").css("color","green");
        $("#Shipped").css("color","green");

        $("#selectAllProduct").css("backgroundColor","white");
        $("#selectAllProduct").css("color","green");
        $("#AllProduct").css("color","green");


        $("#selectUnpaidProduct").css("backgroundColor","white");
        $("#selectUnpaidProduct").css("color","green");
        $("#unpaid").css("color","green");


        $("#selectPaidProduct").css("backgroundColor","white");
        $("#selectPaidProduct").css("color","green");
        $("#Paid").css("color","green");


        $("#selectToBeShippedProduct").css("backgroundColor","white");
        $("#selectToBeShippedProduct").css("color","green");
        $("#ToBeShipped").css("color","green");



        $("#selectCancelTransactionProduct").css("backgroundColor","white");
        $("#selectCancelTransactionProduct").css("color","green");
        $("#CancelTransaction").css("color","green");
        //未付款
        QueryProductOrdersByStatus(param);
    });
    //取消交易
    $("#selectCancelTransactionProduct").click(function () {
        var param = {
            "orderType":"3",
            "orderStatus":7
        }
        $("#selectCancelTransactionProduct").css("backgroundColor","#009688");
        $("#selectCancelTransactionProduct").css("color","#fff");
        $("#CancelTransaction").css("color","#fff");

        $("#selectShippedProduct").css("backgroundColor","white");
        $("#selectShippedProduct").css("color","green");
        $("#Shipped").css("color","green");

        $("#selectAllProduct").css("backgroundColor","white");
        $("#selectAllProduct").css("color","green");
        $("#AllProduct").css("color","green");


        $("#selectUnpaidProduct").css("backgroundColor","white");
        $("#selectUnpaidProduct").css("color","green");
        $("#unpaid").css("color","green");


        $("#selectPaidProduct").css("backgroundColor","white");
        $("#selectPaidProduct").css("color","green");
        $("#Paid").css("color","green");


        $("#selectToBeShippedProduct").css("backgroundColor","white");
        $("#selectToBeShippedProduct").css("color","green");
        $("#ToBeShipped").css("color","green");



        $("#selectSignedInProduct").css("backgroundColor","white");
        $("#selectSignedInProduct").css("color","green");
        $("#SignedIn").css("color","green");
        //未付款
        QueryProductOrdersByStatus(param);
    });

    var param = {
        "priceTotal":$("#priceTotal").val(),
        "paymentTime":$("#paymentTime").val(),
        "orderPaymentChannel":$("#orderPaymentChannel").val(),
        "orderSource":$("#orderSource").val(),
        "orderType":"3"

    };
    //查询全部课程订单、已付款订单、未付款订单、取消订单
    function TotalCourseStatusCalculations(orderType,orderStatus){
        $.ajax({
            url:"/Order/TotalCourseStatusCalculations",
            data:{"orderType":orderType,"orderStatus":orderStatus},
            type:"post",
            dataType:"json",
            success:function (data) {
                console.log(data);
                if (orderStatus == 2){
                    $("#unpaid").text("("+data.data+")");//未付款
                    $("#AllProduct").text(Number($("#AllProduct").text())+Number(data.data));
                }else if(orderStatus == 3){
                    $("#Paid").text("("+data.data+")");//已付款
                    $("#AllProduct").text(Number($("#AllProduct").text())+Number(data.data));
                }else if(orderStatus == 4){
                    $("#ToBeShipped").text("("+data.data+")");//取消交易
                    $("#AllProduct").text(Number($("#AllProduct").text())+Number(data.data));
                }else if(orderStatus == 5){
                    $("#Shipped").text("("+data.data+")");//取消交易
                    $("#AllProduct").text(Number($("#AllProduct").text())+Number(data.data));
                }else if(orderStatus == 6){
                    $("#SignedIn").text("("+data.data+")");//取消交易
                    $("#AllProduct").text(Number($("#AllProduct").text())+Number(data.data));
                }else if(orderStatus == 7){
                    $("#CancelTransaction").text("("+data.data+")");//取消交易
                    $("#AllProduct").text(Number($("#AllProduct").text())+Number(data.data));
                }
            }
        })
    }
    $("#refresh").click(function () {
        location.reload();
    });
    $('#previous').click(function () {
        parent.tabParentChange(3);
    })
    //绑定时间输入框
    laydate.render({
        elem:"#paymentTime",
        type:"datetime"
    })
    //点击搜索查询数据
    $("#search").click(function () {
        var param = {
            "priceTotal":$("#priceTotal").val(),
            "paymentTime":$("#paymentTime").val(),
            "orderPaymentChannel":$("#orderPaymentChannel").val(),
            "orderSource":$("#orderSource").val(),
            "orderType":"3"
        };
        getAllCourseOrder(param);
    });
    //初始化调用、查询所有商品订单信息
    getAllCourseOrder(param);
    function getAllCourseOrder(param){
        $.ajax({
            url:'/Order/getAllCourseOrder',
            data:param,
            type:'post',
            dataType:'json',
            success:function(data){
                //调用table展示数据
                showProductOrder(data.data);
            }
        })
    }
    //重置
    $("#Reset").click(function () {
        $("#priceTotal").val(""),
            $("#paymentTime").val(""),
            $("#orderPaymentChannel").val(0),
            $("#orderSource").val(0)
        form.render();
        getAllCourseOrder(param);
    });
    //高级检索
    $("#AdvancedRetrieval").click(function () {
        layer.open({
            title:"高级检索",
            type:2,
            content:"../courseOrder/courseOrderAdvancedRetrieval.html",
            area:["800px","500px"],
            btn:["确定","取消"],
            yes:function (index, layero) {
                var obj = layero.find('iframe').contents();
                var courseName = obj.find("input[name=courseName]").val();
                var nickname = obj.find("input[name=nickname]").val();
                var paymentTime = obj.find("input[name=paymentTime]").val();
                var deliveryTime = obj.find("input[name=deliveryTime]").val();
                var orderPaymentChannel = obj.find("select[name=orderPaymentChannel]").val();
                var orderSource = obj.find("select[name=orderSource]").val();
                var param = {
                    "courseName":courseName,
                    "nickname":nickname,
                    "paymentTime":paymentTime,
                    "deliveryTime":deliveryTime,
                    "orderPaymentChannel":orderPaymentChannel,
                    "orderSource":orderSource,
                    "orderType":"3"
                };
                if (courseName==null || courseName== "" && nickname==null || nickname=="" && paymentTime==null || paymentTime=="" && deliveryTime== null || deliveryTime== "" && orderPaymentChannel==null || orderPaymentChannel=="" && orderSource==null || orderSource==""){
                    layer.close(index);
                }else{
                    $.ajax({
                        url:"/Order/OrderAdvancedRetrieval",
                        data:param,
                        type:'post',
                        dataType:'json',
                        success:function (data) {
                            showProductOrder(data.data);
                        }
                    })
                    layer.close(index);
                }

            },btn2:function (index, layero) {

            }
        });
    });
    //table表格
    function showProductOrder(data){
        table.render({
            elem:"#tableTest",
            data:data,
            // width:1650,
            page: {
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip']
                ,first: true //不显示首页
                ,last: true //不显示尾页
            },
            cols: [[
                {type:'checkbox'}
                ,{field:'nickname',  title: '用户昵称',align:"center"}
                ,{field:'paymentTime',title: '付款时间',align:"center", sort: true}
                ,{field:'deliveryTime',  title: '发货时间',align:"center", sort: true}
                ,{field:'courseName', title: '课程名称', align:"center"}
                ,{field:'orderPaymentChannel',  title: '支付方式',align:"center",templet:function (data) {
                        if (data.orderPaymentChannel==2){
                            return "微信支付";
                        }else if(data.orderPaymentChannel==3){
                            return "支付宝支付";
                        }
                    }}
                ,{field:'orderSource', title: '订单来源',align:"center",templet:function (data) {
                        if (data.orderSource == 2){
                            return "小程序";
                        }else if(data.orderSource == 3){
                            return "APP";
                        }
                    }} //minWidth：局部定义当前单元格的最小宽度，layui 2.2.1 新增
                ,{field:'orderStatus', title: '订单状态',align:"center",templet:function (data) {
                        //2未付款，3已付款，4待发货，5已发货，6已签收，7退货申请，9已退货，10拒绝退订，11退货中
                        if (data.orderStatus == 2) {
                            return "未付款";
                        }else if(data.orderStatus == 3){
                            return "已付款";
                        }else if(data.orderStatus == 4){
                            return "待发货";
                        }else if(data.orderStatus == 5){
                            return "已发货";
                        }else if(data.orderStatus == 6){
                            return "已签收";
                        }else if(data.orderStatus == 7){
                            return "退货申请";
                        }else if(data.orderStatus == 9){
                            return "已退货";
                        }else if(data.orderStatus == 10){
                            return "拒绝退订";
                        }else if(data.orderStatus == 11){
                            return "退货中";
                        }
                    }}
                ,{field:'priceTotal', title: '商品总价', sort: true,align:"center"}
                ,{field:'receivingAddress', title: '收获地址',align:"center"}
                ,{field:'logisticsFee',  title: '订单金额', sort: true,align:"center"}
                ,{field:'',title: '操作',align:"center",toolbar:"#rowFunction"}
            ]]
        });
        //行内工具栏监听事件
        table.on('tool(tableTest)',function (obj) {
            var objData = obj.data;
            var event = obj.event;
            if (event == "rowSelect") {
                layer.open({
                    title:"商品订单查看",
                    type:2,
                    content:"../courseOrder/courseOrderSelect.html",
                    area:["95%","100%"],
                    btn:["确定","取消"],
                    success:function(layero,index){
                        var obj = layero.find('iframe').contents();
                        obj.find("th[name=courseName]").text(objData.courseName);
                        obj.find("th[name=nickname]").text(objData.nickname);
                        if (objData.orderStatus==2){
                            obj.find("th[name=orderStatus]").text("未付款");
                        } else if (objData.orderStatus==3){
                            obj.find("th[name=orderStatus]").text("已付款");
                        }else if (objData.orderStatus==4){
                            obj.find("th[name=orderStatus]").text("待发货");
                        }else if (objData.orderStatus==5){
                            obj.find("th[name=orderStatus]").text("已发货");
                        }else if (objData.orderStatus==6){
                            obj.find("th[name=orderStatus]").text("已签收");
                        }else if (objData.orderStatus==7){
                            obj.find("th[name=orderStatus]").text("退货申请");
                        }else if (objData.orderStatus==9){
                            obj.find("th[name=orderStatus]").text("已退货");
                        }else if (objData.orderStatus==10){
                            obj.find("th[name=orderStatus]").text("拒绝退订");
                        }else if (objData.orderStatus==11){
                            obj.find("th[name=orderStatus]").text("退货中");
                        }


                        obj.find("th[name=orderLogisticsNumber]").text(objData.orderLogisticsNumber);
                        if (objData.orderPaymentChannel == 2){
                            obj.find("th[name=orderPaymentChannel]").text("微信支付");
                        }else if (objData.orderPaymentChannel == 3){
                            obj.find("th[name=orderPaymentChannel]").text("支付宝支付");
                        }
                        obj.find("th[name=receivingAddress]").text(objData.receivingAddress);
                        obj.find("th[name=paymentTime]").text(objData.paymentTime);
                        obj.find("th[name=deliveryTime]").text(objData.deliveryTime);
                        if(objData.orderSource == 2){
                            obj.find("th[name=orderSource]").text("小程序");
                        }else if (objData.orderSource == 3){
                            obj.find("th[name=orderSource]").text("APP");
                        }
                        obj.find("th[name=remarks]").text(objData.remarks);
                    },
                    yes:function (index,layero) {

                        layer.close(index);

                    },btn2:function (index,layero) {

                    }
                })

            }else if(event == "rowDelete"){
                layer.confirm("确定删除当前订单吗？",{icon:'3',title:"删除"},function (index) {
                    $.ajax({
                        url:"/Order/DeleteOrderById",
                        data:{"orderId":objData.orderId},
                        type:'post',
                        dataType:'json',
                        success:function (data) {
                            if(data.data == 1){
                                layer.msg("删除成功");
                                getAllCourseOrder(param);
                                layer.close(index);
                            }else{
                                layer.msg("删除失败");
                                layer.close(index);
                            }
                        }
                    })
                })
            }
        });

    }
    //根据不同按钮查询不同状态信息
    function QueryProductOrdersByStatus(param) {
        $.ajax({
            url:"/Order/QueryCourseOrdersByStatus",
            data:param,
            type:'post',
            dataType:'json',
            success:function (data) {
                showProductOrder(data.data);
            }
        })
    }

})